package com.alibaba.jingn.controller;

import cn.alibaba.open.param.AlibabaCaigouCoopapiPricingchartParamPricingChartPostParam;
import cn.alibaba.open.param.AlibabaCaigouProductAddOrUpdateErpProductCoopParam;
import cn.alibaba.open.param.CaigouApiAccountCreateSubAccountParam;
import cn.alibaba.open.param.CaigouApiBuyofferPostBuyofferParam;
import com.alibaba.jingn.service.MaterialService;
import com.alibaba.jingn.util.AjaxResult;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * 物资系统
 * @author ZhangXuHao
 * @date 2021/04/18  12:08
 */
@RestController
@RequestMapping("/material")
public class MaterialController extends BaseController{

    @Autowired
    MaterialService materialService;

    /**
     * 创建子帐号
     * @param record 入参
     * @return
     */
    @PostMapping("insrtsonuser")
    public AjaxResult insrtSonUser(@NonNull CaigouApiAccountCreateSubAccountParam record){
        Map<String,Object> map = materialService.insrtSonUser(record);

        if((Boolean)  map.get("success")){
            return AjaxResult.success(map.get("result"));
        }else {
            return AjaxResult.error(map.get("code").toString(),map.get("message"));
        }
    }

    /**
     * 获取比价单详情
     * //直接传  purchaseId
     * @author MeiPan
     * @date 2021/04/18  16:45
     */
    @PostMapping("getdetails")
    public AjaxResult getDetails(String pricingChartId){

        Map<String,Object> map = materialService.detailsParam(pricingChartId);
        if(0 == (int) map.get("msg")){
            if((boolean) map.get("success")){
                return AjaxResult.success(map.get("result"));
            }else{
                return AjaxResult.error(map.get("message").toString());
            }
        }else {
            return AjaxResult.error(map.get("news").toString());
        }
    }


    /**
     * 新增erp类目
     * @param list 入参
     * @return
     */
    @PostMapping("inseterp")
    public AjaxResult inseterp(@NonNull List<Map<String,Object>> list){

        Map<String,Object> map = materialService.inseterp(list);
        if((Boolean)  map.get("success")){
            return AjaxResult.success(map.get("result"));
        }else {
            return AjaxResult.error(map.get("code").toString(),map.get("message"));
        }
    }

    /**
     * 修改erp类目
     * @param alibabaCaigouProductAddOrUpdateErpProductCoopParams 入参
     * @return
     */
    @PostMapping("updateerp")
    public AjaxResult updateerp(@NonNull AlibabaCaigouProductAddOrUpdateErpProductCoopParam[] alibabaCaigouProductAddOrUpdateErpProductCoopParams){
        Map<String,Object> map = materialService.updateerp(alibabaCaigouProductAddOrUpdateErpProductCoopParams);
        if((Boolean)  map.get("success")){
            return AjaxResult.success(map.get("data"));
        }else {
            return AjaxResult.error(map.get("code").toString(),map.get("message"));
        }
    }

    /**
     * 发布询价单
     * @param record 入参
     * @return
     */
    @PostMapping("getinquiry")
    public AjaxResult getinquiry(@NonNull CaigouApiBuyofferPostBuyofferParam record){
        Map<String,Object> map = materialService.inquiryparam(record);
        if((Boolean) map.get("success")){
            return AjaxResult.success(map.get("orderId"));
        }else {
            return AjaxResult.error(map.get("code").toString(),map.get("message"));
        }
    }

    /**
     * 比价单审核和审批
     * @param record 入参
     * @return
     */
    @PostMapping("comparisonApproval")
    public AjaxResult comparisonApproval(AlibabaCaigouCoopapiPricingchartParamPricingChartPostParam record){

        Map<String,Object> map = materialService.comparisonApproval(record);
        if((Boolean) map.get("success")){
            return AjaxResult.success(map.get("data"));
        }else {
            return AjaxResult.error(map.get("code").toString(),map.get("message"));
        }
    }

    /**
     * 比价单审核和审批
     * @param procurementOrderId 入参
     * @return
     */
    @PostMapping("queryOrder")
    public AjaxResult queryOrder(Long procurementOrderId){
        Map<String,Object> map = materialService.queryOrder(procurementOrderId);
        if((Boolean) map.get("success")){
            return AjaxResult.success(map.get("data"));
        }else {
            return AjaxResult.error(map.get("code").toString(),map.get("message"));
        }
    }

    /**
     * 查询供应商会员id
     * @param outCodes 入参
     * @return
     */
    @PostMapping("querySupplier")
    public AjaxResult querySupplier(String[] outCodes){
        Map<String,Object> map = materialService.querySupplier(outCodes);
        if((Boolean) map.get("success")){
            return AjaxResult.success(map.get("data"));
        }else {
            return AjaxResult.error(map.get("code").toString(),map.get("message"));
        }
    }

}